angular.module('todoApp').controller('todoController', function($scope) {
  $scope.todoList = [{ 
    content: 'write a doc',
    time: new Date().toLocaleString()
  }];

  $scope.addItem = function(content) {
    $scope.todoList.push({
      content,
      checked: false,
      time: new Date().toLocaleString()
    });
  };
  $scope.onChangeOrder = function(ascend) {
    $scope.todoList = $scope.todoList.sort((a, b) => {
      if (a.time === b.time) return 0;
      if (ascend) {
        return a.time < b.time ? 1 : -1;
      } else {
        return a.time > b.time ? 1 : -1;
      }
    });
  };
});